package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.MediaMapper;
import com.showcai.def.Constant;
import com.showcai.module.Media;
import com.showcai.module.MediaPushLog;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ReturnCode;
import com.showcai.service.MediaPushLogService;
import com.showcai.service.MediaService;
import com.showcai.service.ServiceBase;
import com.showcai.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-8-15.
 */
@Service
public class MediaServiceImpl extends ServiceBase implements MediaService {
    @Autowired
    MediaMapper mediaMapper;
    @Autowired
    MediaPushLogService mediaPushLogService;
    @Autowired
    UserService userService;

    @Override
    public List<Media> selectMediaListBydevicId(String devicId) throws MyRunTimeExcption {
        List<Media> mediaList = mediaMapper.selectPushMediaList(devicId);
        return mediaList;
    }

    @Override
    public PageInfo getMediaList(Map<String, String> map) {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);


        List<Media> mediaList = mediaMapper.selectMediaList(map);

        PageInfo<Media> pageInfo = new PageInfo<>(mediaList);

        return pageInfo;
    }

    @Override
    public Media getMediaById(Long id) {
        if (id == null) {
            throw new MyRunTimeExcption(ReturnCode.MEDIA_ID_IS_NULL);
        }
        Media media = mediaMapper.selectMediaById(id);
        if (media == null) {
            throw new MyRunTimeExcption(ReturnCode.MEDIA_CONTENT_IS_NULL);
        }
        return media;
    }

    @Override
    public Media pushMediaById(Long id, String devicId, String memberId) throws MyRunTimeExcption {
        if (id == null) {
            throw new MyRunTimeExcption(ReturnCode.MEDIA_ID_IS_NULL);
        }
        Media media = mediaMapper.selectMediaById(id);
        if (media == null) {
            throw new MyRunTimeExcption(ReturnCode.MEDIA_CONTENT_IS_NULL);
        }
        media.setDevicId(devicId);
        User user = userService.getUserByThirdId(memberId);
        if (user == null) {
            throw new MyRunTimeExcption(ReturnCode.USER_IS_INEXISTENCE);
        }
        Long userId = user.getTid();

        //添加push日志记录
        MediaPushLog mediaPushLog = new MediaPushLog();
        mediaPushLog.setDevicId(devicId);
        mediaPushLog.setMediaId(media.getTid());
        mediaPushLog.setUserId(userId);
        mediaPushLog.setCreateTime(new Date());
        mediaPushLogService.addMediaPushLog(mediaPushLog);
        return media;
    }

    @Override
    public void modifyMedia(Media media) {
        if (media.getTid() == null) {
            insert(media);
            return;
        }
        update(media);
    }

    private Media insert(Media media) {
        media.setCreateTime(new Date());
        mediaMapper.insertMedia(media);
        return media;
    }

    private void update(Media media) {
        mediaMapper.updateMediaByPrimaryKeySelective(media);
    }

    @Override
    public List<Media> selectMediaList(Map<String, String> map) throws MyRunTimeExcption {
        List<Media> mediaList = mediaMapper.selectPushMediaListByclassify(map);
        return mediaList;
    }

}
